package com.baofu.paycore.biz.engine.generator.impl;

import com.baofu.paycore.biz.engine.command.Command;
import com.baofu.paycore.biz.engine.generator.CommandGenerator;
import com.baofu.paycore.biz.paycore.converter.CommandConverter;
import com.baofu.paycore.common.enums.FundTypeEnum;
import com.baofu.paycore.manager.model.RefundBO;
import com.baofu.paycore.manager.model.SubRefundBO;
import com.baofu.paycore.service.facade.enums.PayToolRouteEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 退款支付指令生成
 * <p>
 * </p>
 * User: kwl Date: 2016/6/12 ProjectName: paycore Version: 5.0.0
 */
@Slf4j
@Service
public class RefundGenerator implements CommandGenerator<RefundBO> {

    /**
     * 生成指令
     *
     * @param refundBO        订单模型
     * @return                支付指令列表
     */
    @Override
    public List<Command> generatorCommands(RefundBO refundBO) {

        //指令集合
        List<Command> commands = new ArrayList<>();

        List<SubRefundBO> subRefundBOs = refundBO.getSubRefundBOs();

        for (SubRefundBO subRefundBO : subRefundBOs) {

            //退款 收手续费指令生成
            if(FundTypeEnum.FEE.getCode().equals(subRefundBO.getFundType())){

                commands.add(CommandConverter.balanceCommandConverter(subRefundBO, refundBO));
                continue;
            }

            //退款指令生成
            if(PayToolRouteEnum.ACCOUNT_TRANSFER_ROUTE.containsPayTool(subRefundBO.getPayToolType())) {

                commands.add(CommandConverter.refundBalanceCommandConverter(subRefundBO, refundBO));

            }
            if(PayToolRouteEnum.CLEAR_WEBPAY_ROUTE.containsPayTool(subRefundBO.getPayToolType())) {

                commands.add(CommandConverter.refundBankCommandConverter(subRefundBO, refundBO));
            }
        }
        return commands;
    }

}
